package cn.pku.sirx.snad.uias.analyzer.similarity;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import cn.pku.sirx.snad.uias.model.entity.SiteUser;
import cn.pku.sirx.snad.uias.util.AnalyzerUtils;

/**
 * @author WangXiao
 */
public class InterestingAnalyzer implements ISimilarityAnalyzer {

	@Override
	public int analyze(SiteUser fromUser, SiteUser toUser) {
		Assert.notNull(fromUser);
		Assert.notNull(toUser);

		double[] valueVector = {
				analyze(fromUser.getInterests(), toUser.getInterests()),
				analyze(fromUser.getGroups(), toUser.getGroups()),
				analyze(fromUser.getFavorBooks(), toUser.getFavorBooks()),
				analyze(fromUser.getFavorCartoon(), toUser.getFavorCartoon()),
				analyze(fromUser.getFavorMovies(), toUser.getFavorMovies()),
				analyze(fromUser.getFavorMusics(), toUser.getFavorMusics()),
				analyze(fromUser.getFavorSports(), toUser.getFavorSports()) };
		return (int) AnalyzerUtils.analyzeByWeight(weightVector, valueVector);
	}

	@SuppressWarnings("unchecked")
	private int analyze(String value1, String value2) {
		if (value1 == null || value2 == null) {
			return 0;
		}
		List<String> fromClassList = Arrays.asList(StringUtils.split(value1,
				SEPARATORS));
		List<String> toClassList = Arrays.asList(StringUtils.split(value2,
				SEPARATORS));
		return AnalyzerUtils.getCommonCount(fromClassList, toClassList,
				ComparatorUtils.NATURAL_COMPARATOR);
	}

	private final String SEPARATORS = ".()+ ,;，；和与";
	/*
	 * interesting, group, favor book, favor cartoon, favor movie, favor music,
	 * favor sport
	 */
	private double[] weightVector = { 1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.5 };

}
